using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using System.Collections.ObjectModel;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Windows.Threading;
using System.Threading;
using MusicPlayerAddIn.Properties;
using ViewOnCore.Helper.WPF;
using ViewOnCore.HostContract;
using ViewOnCore.HostContract.Media;
using ViewOnCore.Device;

namespace UI.Control
{

    public partial class KioskAlbumDialog : System.Windows.Controls.UserControl
    {

        #region Variables

        private string _album;
        private AlbumCoverItem _item;

        private ObservableCollection<Music> _musics = new ObservableCollection<Music>();

        #endregion

        #region Constructor

        public KioskAlbumDialog(AlbumCoverItem item)
        {
            InitializeComponent();

            _item = item;
            _album = item.Key;

            imgCover.Source = _item.BitmapSource;
            txtTitle1.Text = item.Title01;
            txtTitle2.Text = item.Title02;

            lstAlbums.ItemsSource = _musics;
            lstAlbums.MouseDoubleClick += new MouseButtonEventHandler(lstAlbums_MouseDoubleClick);

            childWindowDecorator.VisuallyReady += new EventHandler(childWindowDecorator_VisuallyReady);
        }

        void childWindowDecorator_VisuallyReady(object sender, EventArgs e)
        {
            Thread thread = new Thread(new ThreadStart(BuildList));
            thread.Priority = ThreadPriority.Lowest;
            thread.Start();
        }

        #endregion

        #region BuildList

        private void BuildList()
        {
            double rate = 0;
            int count = 0;
            foreach (Music media in Host.MediaService.CurrentMediaDeviceFeature.MediaDatas)
                if (media.Album.Equals(_album, StringComparison.OrdinalIgnoreCase))
                    Dispatcher.Invoke(DispatcherPriority.Background, (DelegateInvoker)delegate
                    {
                        _musics.Add(media);
                        rate += media.Rating;
                        count++;
                    });

            rate = (int)(rate / count);

            Dispatcher.Invoke(DispatcherPriority.Background, (DelegateInvoker)delegate
            {
                tbtRating1.IsChecked = (rate >= 1);
                tbtRating2.IsChecked = (rate >= 2);
                tbtRating3.IsChecked = (rate >= 3);
                tbtRating4.IsChecked = (rate >= 4);
                tbtRating5.IsChecked = (rate >= 5);
            });
        }

        #endregion

        #region Exit

        private void btnExit_Click(object sender, System.Windows.RoutedEventArgs e)
        {
            this.childWindowDecorator.Close();
        }

        #endregion

        #region ShowDialog

        /// <summary>
        /// Show the dialog.
        /// </summary>
        /// <param name="container">The desktop</param>
        /// <param name="handler">This handler handle when the dialog is closed</param>
        public void ShowDialog(Desktop container, ChildWindowDialogClosedHandler handler)
        {
            childWindowDecorator.ShowDialog(container, handler);
        }

        /// <summary>
        /// Show the dialog.
        /// </summary>
        /// <param name="container">The desktop</param>
        public void ShowDialog(Desktop container)
        {
            childWindowDecorator.ShowDialog(container);
        }

        #endregion

        #region MouseDoubleClick

        void lstAlbums_MouseDoubleClick(object sender, MouseButtonEventArgs e)
        {
            Music music = lstAlbums.SelectedValue as Music;
            if (music == null)
                return;

            Host.MediaService.PlayMedia(music);
        }

        #endregion

        #region  Button play

        public void btnPlay_Click(object sender, RoutedEventArgs args)
        {
            lstAlbums.SelectedItem = (sender as Button).DataContext;

            Music music = lstAlbums.SelectedValue as Music;
            if (music == null)
                return;

            Host.MediaService.PlayMedia(music);
        }

        #endregion

    }

}